package com.magina.common.utils;

import org.springframework.util.StopWatch;

/**
 * @ClassnName :com.magina.common.utils.StopWatchUtil
 * @Author： Magina.Hu
 * @Date： 2019/6/11
 * @Description： 计时器测试程序耗时
 * @Version: V1.0
 * Copyright (c) 2019-2020 .All rights reserved.
 */
public class StopWatchUtil {
    public static void main(String[] args) throws InterruptedException {

        testStopWatch();
//        testCurrentTime();
    }

    private static void testCurrentTime() throws InterruptedException {
        long startTime = System.currentTimeMillis();
        Thread.sleep(1111L);
        long endTime = System.currentTimeMillis();
        long costTime = endTime - startTime;
        System.err.println("该段代码耗时：" + costTime + " ms");
    }

    private static void testStopWatch() throws InterruptedException {
        // 定义一个计数器
        StopWatch stopWatch = new StopWatch("统一一组任务耗时");
        // 统计任务一耗时
        stopWatch.start("任务一");
        Thread.sleep(1000);
        stopWatch.stop();
        // 统计任务二耗时
        stopWatch.start("任务二");
        Thread.sleep(2000);
        stopWatch.stop();
        // 打印出耗时
        String result = stopWatch.prettyPrint();
        System.err.println(result);
    }
}
